<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <AjaxButton :click="saveDialog" type="primary">确 定</AjaxButton>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
import {
  driveCancel,
  cancelReason
 } from '@/api/potenCus/customer/driveManagement';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'CancelDrive',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'cancel'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
    };
  },
  created() {
    this.init(),
    this.getCancelReason()
  },
  methods: {
    init() {
      if (this.type === 'cancel' ) {
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '取消试驾',
          fieldName: 'cancelExplain',
          placeholder: '请选择',
          style: { width: '200px' },
          itemList: [],
          rules: [{ required: true, message: '请选择取消试驾原因', trigger: 'blur' }],
          change: val => {
            this.changeCancel(val);
          }
        },
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'cancel') {
          res = await driveCancel({ ...this.saveData, itemId: this.rowData.itemId, planStatus: 20211002 });
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    async getCancelReason() {
      const res = await cancelReason();
      // console.log('取消试驾原因', res)
      if (res.resultCode === 200) {
        this.formList[0].itemList = res.data.map(x => ({ value: x.cancelCauseId, text: x.cancelExplain }));
      }
    },
    changeCancel(cancelExplain) {
      if (cancelExplain == '2') {
        this.formList.push(
          {
            type: 'TEXT_AREA',
            label: '备注',
            fieldName: 'cancelCauseRemark',
            placeholder: '请备注取消原因',
            rows: 2,
            style: { width: '200px' },
            rules: [{ required: true, message: '请备注原因', trigger: 'blur' }]
          },
        )
      } else {
        this.formList.splice(1,1)
      }
      this.formList = [ ...this.formList ]
    },
  }
};
</script>

<style lang="less" scoped>
</style>

